//
//  LPNewsCommentView.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 2017/6/2.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPNewsCommentView.h"

@interface LPNewsCommentView ()<UITextFieldDelegate,UITextViewDelegate>
/**  textField  */
@property (strong, nonatomic) UITextField  *shuruTextField;
/**  评论详情图片  */
@property (strong, nonatomic) UIImageView  *commentImageView;
/**  分享  */
@property (strong, nonatomic) UIImageView  *shareImageView;
/**  发送  */
@property (strong, nonatomic) UIButton  *sendButton;
/**  textView  */
@property (strong, nonatomic) UITextView  *importTextView;
@end
/**  49  */
@implementation LPNewsCommentView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self layoutUI];
    }
    return self;
}




#pragma mark - 界面
- (void)layoutUI{
    UIView *top = [UIView new];
    top.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:top];
    top.sd_layout
    .leftEqualToView(self)
    .rightEqualToView(self)
    .heightIs(1)
    .widthIs([UIScreen zj_screenWith]);
    
    [self addSubview:self.importTextView];
    [self addSubview:self.sendButton];
    [self addSubview:self.commentImageView];
    [self addSubview:self.shareImageView];
    

}


#pragma mark - 私有方法


#pragma mark - 监听事件
- (void)clickButton{
    if (self.delegate) {
        [self.delegate LPNewsCommentWithView:self text:self.importTextView.text];
    }
    
    self.importTextView.text = @"";
    
    if (self.delegate) {
        [self.delegate lpCustomImportHeightWithView:self textViewHeight:31];
    }
    
    /**  重置高度  */
    [self setViewHeight:35];
    [self.importTextView resignFirstResponder];
}

- (void)tapImageView:(UITapGestureRecognizer *)tap{
    if (self.delegate) {
        [self.delegate LPNewsCommentWithView:self index:tap.view.tag];
    }
}

#pragma mark - 协议代理
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        if (self.delegate) {
            [self.delegate LPNewsCommentWithView:self text:textView.text];
        }
        textView.text = @"";
        [textView resignFirstResponder];
        
        return NO;
        
    }
    
    return YES;    
    
}


- (void)textViewDidChange:(UITextView *)textView{
    CGFloat textViewMinHeight = 35; // TextView最小高度
    CGFloat textViewMaxHeight = 85; // TextView最大高度
    // 重要属性
    CGFloat textViewContentH = textView.contentSize.height;
    CGFloat textHeight;
    if (textViewContentH < textViewMinHeight) {
        textHeight = 35;
    }else if (textViewContentH > textViewMaxHeight){
        textHeight = 85;
    }else{
        textHeight = textViewContentH;
    }
    if (self.delegate) {
        [self.delegate lpCustomImportHeightWithView:self textViewHeight:textHeight];
    }
    [self setViewHeight:textHeight];
    
}


/**  重置高度  */
- (void)setViewHeight:(CGFloat)textViewHieght{
    /**  重置高度  */
    CGRect frame = self.importTextView.frame;
    frame.size.height = textViewHieght;
    self.importTextView.frame = frame;
    
    CGFloat heigth = textViewHieght - 35;
    if (heigth == 0) {
        
        CGRect frame1 = self.sendButton.frame;
        frame1.origin.y = 9;
        self.sendButton.frame = frame1;
        
        CGRect frame2 = self.commentImageView.frame;
        frame2.origin.y = 9;
        self.commentImageView.frame = frame2;
        
        CGRect frame3 = self.shareImageView.frame;
        frame3.origin.y = 9;
        self.shareImageView.frame = frame3;

        
    }else{
        CGRect frame1 = self.sendButton.frame;
        frame1.origin.y = 9 + heigth / 2;
        self.sendButton.frame = frame1;
        
        CGRect frame2 = self.commentImageView.frame;
        frame2.origin.y = 9 + heigth / 2;
        self.commentImageView.frame = frame2;
        
        CGRect frame3 = self.shareImageView.frame;
        frame3.origin.y = 9 + heigth / 2;
        self.shareImageView.frame = frame3;
    }
    
    
}

#pragma mark - getter

- (UITextView *)importTextView{
    if (!_importTextView) {
        _importTextView = [[UITextView alloc] initWithFrame:CGRectMake(15 * [UIScreen zj_screenWScale], 7, [UIScreen zj_screenWith] - 110 * [UIScreen zj_screenWScale] - 62, 35)];
        _importTextView.delegate = self;
        _importTextView.layer.cornerRadius = 4;
        _importTextView.layer.masksToBounds = YES;
        _importTextView.layer.borderWidth = 1;
        _importTextView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _importTextView.returnKeyType = UIReturnKeySend;
    }
    return _importTextView;
    
}


- (UIButton *)sendButton{
    if (!_sendButton) {
        _sendButton = ({
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.importTextView.frame) + 10 * [UIScreen zj_screenWScale], 9, 50 * [UIScreen zj_screenWScale], 31)];
            [button setTitle:@"评论" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            button.layer.cornerRadius = 6;
            button.layer.masksToBounds = YES;
            button.layer.borderWidth = 1;
            button.titleLabel.font = [UIFont systemFontOfSize:16 * [UIScreen zj_textFontScale]];
            button.layer.borderColor = [UIColor lightGrayColor].CGColor;
            [button addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
            button;
            
        });
    }
    return _sendButton;
}


- (UIImageView *)commentImageView{
    if (!_commentImageView) {
        _commentImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.sendButton.frame) + 10 * [UIScreen zj_screenWScale], 9, 31, 31)];
        _commentImageView.contentMode = UIViewContentModeCenter;
        _commentImageView.backgroundColor = [UIColor redColor];
        _commentImageView.tag = 2;
        _commentImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
        [_commentImageView addGestureRecognizer:tap];
    }
    return _commentImageView;

}

- (UIImageView *)shareImageView{
    if (!_shareImageView) {
        _shareImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.commentImageView.frame) + 10 * [UIScreen zj_screenWScale], 9, 31, 31)];
        _shareImageView.contentMode = UIViewContentModeCenter;
        _shareImageView.backgroundColor = [UIColor greenColor];
        _shareImageView.tag = 1;
        _shareImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
        [_shareImageView addGestureRecognizer:tap];

    }
    return _shareImageView;
    
}

@end
